package DEEPERsource.DEEPERsource.source.client;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import preprocessing.DefaultPreprocessor;
import preprocessing.SimplePreprocessor;
import corpus.Text;
import corpus.ppi.CorpusFactory;
import deeper.Interaction;
import deeper.InteractionType;

import net.sf.javaml.classification.evaluation.PerformanceMeasure;
import machinelearning.SVM;
import machinelearning.evaluation.EvaluationProcedure;
import machinelearning.features.FeatureBuilder;
import machinelearning.wekawrapper.Dataset;

public class Evaluation {
	
	private static Log _log = LogFactory.getLog(Evaluation.class);
	
	public static void main(String[] args){
		CorpusFactory cFactory = new CorpusFactory();
		Text llltest = cFactory.getCorpusFromPPIXML("resource/llltest.xml", new SimplePreprocessor());
		llltest.parse(null,"serialized","resource/sparse/llltest");
		Collection<Interaction> test = llltest.interactions();
				
		Dataset trainData = new Dataset("resource/kernels/lll/lllTrainKernel");
		Dataset testData = new Dataset("resource/kernels/lll/lllTestKernel");
		testData.addData(test);
		EvaluationProcedure eval = new EvaluationProcedure();
		SVM svm = new SVM();
		try {
			svm.setOptions(new String[]{"-K","4","-B","1"});
			
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		String out = eval.SVMEvaluationForLLL(trainData, testData, svm, InteractionType.FALSE.ordinal(), InteractionType.REALSTRAIGHT.ordinal(), InteractionType.REALINVERSE.ordinal());
		FileWriter writer = null;
		try{
			writer = new FileWriter("resource/lllout");
			writer.append(out);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(writer!=null)
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		
		
		
		/*int folds = 10;
		PerformanceMeasure[] pm = new PerformanceMeasure[folds];
		PerformanceMeasure pmSum = new PerformanceMeasure();
		LibSVM svm = new LibSVM();
		for(int i = 1; i<=folds; i++){
			pm[i-1] = eval.SVMEvaluation(
				"resource/kernels/aimed/aimedTrainKernel"+Integer.toString(i-1), 
				"resource/kernels/aimed/aimedTestKernel"+Integer.toString(i-1), 
				svm, 
				"-t 4 -b 1 -c 10 -w0 1 -w1 5",
				1, 0);
			_log.info("FOLD "+i+"\n\tRecall = "+pm[i-1].getRecall()+
						"\n\tPrecision = "+pm[i-1].getPrecision()+
						"\n\tF-measure = "+pm[i-1].getFMeasure()+
						"\n\tFP rate = "+pm[i-1].getFPRate()+
						"\n\tFN rate = "+pm[i-1].getFNRate()+"\n\n");
			pmSum.falseNegatives += pm[i-1].falseNegatives;
			pmSum.falsePositives += pm[i-1].falsePositives;
			pmSum.truePositives += pm[i-1].truePositives;
			pmSum.trueNegatives += pm[i-1].trueNegatives;
		}
		FileWriter writer = null;
		try {
			File result = new File("resource/results/aimed/final.result");
			writer = new FileWriter(result);
			for(int i = 0; i<folds; i++){
				writer.append(pm[i].toString()).append("\n");
			}
			writer.append("\n\n\tRecall = "+pmSum.getRecall()+
					"\n\tPrecision = "+pmSum.getPrecision()+
					"\n\tF-measure = "+pmSum.getFMeasure()+
					"\n\tFP rate = "+pmSum.getFPRate()+
					"\n\tFN rate = "+pmSum.getFNRate());
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(writer!=null)
					writer.close();				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}*/
	}
}
